package com.fingard.dsp.bank.reqrespobj.single;

import com.fingard.FGBiz;
import com.fingard.dsp.core.specialdeal.PayStateHelper;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RetAtsBase;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.xml.XmlTextWriter;

//单笔合批响应对象
public class RetAtsSingleBat extends RetAtsBase {
	public String totalNum;
    public String totalAmt;
    public String reqBatchNO;
    public String batRespCode;
    public String batRespInfo;
    public String batTransState;
    public String batPayInfoCode;
    public String batBizState;

    public RetAtsSingleBat(ReqRespHeader p_reqHeader) {
        retHeader = p_reqHeader;
        retXmlWriter = new XmlTextWriter();//用于输出返回ATS的XML报文
    }

    public RetAtsSingleBat(ReqSingleBat p_reqBat) {
        retHeader = p_reqBat.reqHeader;
        retXmlWriter = new XmlTextWriter();//用于输出返回ATS的XML报文

        totalNum = p_reqBat.totalNum;
        totalAmt = p_reqBat.totalAmt;
        reqBatchNO = p_reqBat.reqBatchNO;
    }

    private int writeRdCount = 0;

    public int getWrittenRdCount() {
        return writeRdCount;
    }

    public void writeRetDetail(RetAtsSglDetail p_retDetail) throws Exception {
        if (!hasWrittenBefore) {
            writeBeforeDetail();
        }
        writeRdCount++;
        retXmlWriter.writeStartElement("DetailRecord");
        if (FGBiz.dspCenter.convertPayState && p_retDetail.respCode.equals("0")) {
            PayStateHelper payStateHelper = new PayStateHelper(retHeader.bankCode, retHeader.transType, p_retDetail.respInfo, p_retDetail.transState, p_retDetail.payInfoCode);
            payStateHelper.toBank();
            p_retDetail.transState = payStateHelper.payState;
            p_retDetail.payInfoCode = payStateHelper.payInfo;
        }
        retXmlWriter.writeElementString("RespCode", p_retDetail.respCode);
        retXmlWriter.writeElementString("RespInfo", p_retDetail.respInfo);
        retXmlWriter.writeElementString("TransState", p_retDetail.transState);
        retXmlWriter.writeElementString("ReqSeqID", p_retDetail.reqSeqID);
        retXmlWriter.writeElementString("PayInfoCode", p_retDetail.payInfoCode);
        retXmlWriter.writeElementString("TransAmt", p_retDetail.transAmt);
        retXmlWriter.writeEndElement();
    }

    private boolean hasWrittenBefore = false;


    /*
    <TotalNum></TotalNum>
   <TotalAmt></TotalAmt>
   <ReqBatchNO></ReqBatchNO>
   <BatRespCode></BatRespCode>
   <BatRespInfo></BatRespInfo>
   <BatTransState></BatTransState>
   <BatPayInfoCode></BatPayInfoCode>
   <BatBizState></BatBizState>*/

    public void writeBeforeDetail() throws Exception {
        if (hasWrittenBefore) {
            throw new Exception("writeBeforeDetail已被调用过");
        }
        writeStart();//写返回ATS的统一头部信息
        if (retHeader.transType.equals("1908BAT")) {
            retXmlWriter.writeElementString("TotalNum", totalNum);
            retXmlWriter.writeElementString("TotalAmt", totalAmt);
        }
        retXmlWriter.writeElementString("ReqBatchNO", reqBatchNO);
        retXmlWriter.writeElementString("BatRespCode", batRespCode);
        retXmlWriter.writeElementString("BatRespInfo", batRespInfo);
        if (FGBiz.dspCenter.convertPayState && String.valueOf(batRespCode).equals("0")) {
            PayStateHelper payStateHelper = new PayStateHelper(retHeader.bankCode, retHeader.transType, batRespInfo, batTransState, batPayInfoCode);
            payStateHelper.toBank();
            batTransState = payStateHelper.payState;
            batPayInfoCode = payStateHelper.payInfo;
        }
        retXmlWriter.writeElementString("BatTransState", batTransState);
        retXmlWriter.writeElementString("BatPayInfoCode", batPayInfoCode);
        retXmlWriter.writeElementString("BatBizState", batBizState);
        hasWrittenBefore = true;
    }

    public void writeAfterDetail() throws Exception {
        if (!hasWrittenBefore) {
            writeBeforeDetail();
        }
        writeEnd();//写结束
    }
}
